package com.chinaedu.oc.controller.commodity;

import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinaedu.oc.constant.Constant;
import com.chinaedu.oc.po.auditrecord.Auditrecord;
import com.chinaedu.oc.po.commodity.Commodity;
import com.chinaedu.oc.po.commodity.CommodityProduct;
import com.chinaedu.oc.po.commodity.dto.AuditDto;
import com.chinaedu.oc.po.commodity.dto.CommodityBaseDto;
import com.chinaedu.oc.po.commodity.dto.CommodityListBaseDto;
import com.chinaedu.oc.po.commodity.dto.QueryCondition;
import com.chinaedu.oc.po.dto.PubOutputDto;
import com.chinaedu.oc.serv.commondity.ICommodityServ;
import com.chinaedu.oc.util.common.JsonUtil;

@Controller
@RequestMapping("/commodity/")
public class CommodityController {

	@Autowired
	private ICommodityServ iCommodityServ;

	/**
	 * 同步商品时增加或则更新
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "synchronizeCommodityOrUpdate")
	public @ResponseBody PubOutputDto synchronizeCommodityOrUpdate(String data) {

		Commodity commodity = JsonUtil.toBean(data, Commodity.class);

		PubOutputDto pubOutputDto = new PubOutputDto();
		try {
			if(StringUtils.isEmpty(commodity.getId())) {
				pubOutputDto.setMsg("添加成功");
			}else {
				pubOutputDto.setMsg("修改成功");
			}
			iCommodityServ.synchronizeCommodityOrUpdate(commodity);
			pubOutputDto.setStatus(Constant.TRUE);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			pubOutputDto.setStatus(Constant.FALSE);
			if(StringUtils.isEmpty(commodity.getId())) {
				pubOutputDto.setMsg("添加失败");
			}else {
				pubOutputDto.setMsg("修改成功");
			}
		}
		return pubOutputDto;
	}
	
	/**
	 * 增加或则更新
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "addOrUpdate")
	public @ResponseBody PubOutputDto addOrUpdateCommodity(String data) {
		PubOutputDto pubOutputDto = new PubOutputDto();
		Commodity commodity = JsonUtil.toBean(data, Commodity.class);
		
		//打包的商品至少大于等于2
		if(commodity.getCommodityRelationship() == null) {
			pubOutputDto.setStatus(Constant.FALSE);
			pubOutputDto.setMsg("添加失败");
			return pubOutputDto;
		}else {
			if(commodity  .getCommodityRelationship().size() < 2) {
				pubOutputDto.setStatus(Constant.FALSE);
				pubOutputDto.setMsg("添加失败, 至少打包2个商品");
				return pubOutputDto;
			}
		}
		try {
			if(StringUtils.isEmpty(commodity.getId())) {
				pubOutputDto.setMsg("添加失败");
			}else {
				pubOutputDto.setMsg("修改成功");
			}
			iCommodityServ.insertOrUpdate(commodity);
			pubOutputDto.setStatus(Constant.TRUE);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			pubOutputDto.setStatus(Constant.FALSE);
			if(StringUtils.isEmpty(commodity.getId())) {
				pubOutputDto.setMsg("添加成功");
			}else {
				pubOutputDto.setMsg("修改成功");
			}
		}
		return pubOutputDto;
	}	
	

	/**
	 * 定价时查询商品信息
	 * 
	 * @param id
	 */
	@RequestMapping("findById")
	@SuppressWarnings("unchecked")
	public @ResponseBody CommodityBaseDto<Commodity> findById(String data) {
		CommodityBaseDto<Commodity> baseDto = new CommodityBaseDto<Commodity>();
		Commodity commodity = null;
		Map<String, String> map = JsonUtil.toBean(data, Map.class);
		String id = (String) map.get("commodityId");
		try {
			commodity = iCommodityServ.findById(id);
			baseDto.setStatus(Constant.TRUE);
			baseDto.setData(commodity);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			baseDto.setStatus(Constant.FALSE);
			baseDto.setMsg("查询失败");
		}
		return baseDto;
	}

	/**
	 * 上架商品  传入商品ID：  commodityId    上下架状态码  ： group
	 * 
	 * @param id
	 */
	@RequestMapping("group")
	@SuppressWarnings("unchecked")
	public @ResponseBody PubOutputDto group(String data) {
		PubOutputDto pubOutputDto = new PubOutputDto();
		Map<String, Object> map = JsonUtil.toBean(data, Map.class);
		String id = (String) map.get("commodityId");
		Integer groupCode =  (Integer)map.get("group");
		try {
			Commodity commodity = new Commodity();
			commodity.setId(id);
			commodity.setIsGrounding(groupCode);
			iCommodityServ.group(commodity);
			pubOutputDto.setStatus(Constant.TRUE);
			if(Constant.COMMODITY_IS_GROUP_C == groupCode) {
				pubOutputDto.setMsg("上架成功");
			}else {
				pubOutputDto.setMsg("下架成功");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			pubOutputDto.setStatus(Constant.FALSE);
			if(groupCode.equals(Constant.COMMODITY_IS_GROUP_B)) {
				pubOutputDto.setMsg("上架成功");
			}else {
				pubOutputDto.setMsg("下架成功");
			}
		}
		return pubOutputDto;
	}

	/**
	 * 删除商品
	 * 
	 * @param ids
	 */
	@RequestMapping("delete")
	public @ResponseBody PubOutputDto delete(String data) {
		List<String> ids = JsonUtil.toList(data, String.class);
		PubOutputDto pubOutputDto = new PubOutputDto();
		try {
			iCommodityServ.delete(ids);
			pubOutputDto.setStatus(Constant.TRUE);
			pubOutputDto.setMsg("删除成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			pubOutputDto.setStatus(Constant.FALSE);
			pubOutputDto.setMsg("删除失败");
		}
		return pubOutputDto;
	}

	/**
	 * 提交审核
	 * 
	 * @param ids
	 */
	@RequestMapping("audit")
	public @ResponseBody PubOutputDto audit(String data) {
		//List<String> ids = JsonUtil.toList(data, String.class);
		AuditDto auditDto = JsonUtil.toBean(data, AuditDto.class);
		PubOutputDto pubOutputDto = new PubOutputDto();
		try {
			List<String> commitAudit = iCommodityServ.commitAudit(auditDto.getIds(),auditDto.getSponsor());
			if(commitAudit.size() > 0) {
				String commodityName = "";
				for (String string : commitAudit) {
					commodityName += string + "   ";
				}
				pubOutputDto.setStatus(Constant.FALSE);
				pubOutputDto.setMsg("不能提审的商品有:"+ commodityName);
			}else {
				pubOutputDto.setStatus(Constant.TRUE);
				pubOutputDto.setMsg("提审成功");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			pubOutputDto.setStatus(Constant.FALSE);
			pubOutputDto.setMsg("提审失败");
		}
		return pubOutputDto;
	}

	/**
	 * 商品复制
	 * 
	 * @param ids
	 */
	@RequestMapping("copy")
	public @ResponseBody PubOutputDto copy(String data) {
		List<String> ids = JsonUtil.toList(data, String.class);
		PubOutputDto pubOutputDto = new PubOutputDto();
		try {
			
			iCommodityServ.copy(ids);
			pubOutputDto.setStatus(Constant.TRUE);
			pubOutputDto.setMsg("复制成功");
		} catch (Exception e) {
			// TODO: handle exception
			pubOutputDto.setStatus(Constant.FALSE);
			pubOutputDto.setMsg("复制失败");
		}
		return pubOutputDto;
	}

	/**
	 * 查询商品列表 参数: queryCondition: 查询条件全部封装到实体中 分页查询
	 * @param queryCondition
	 * @return
	 */
	@RequestMapping("findCommodityList")
	public @ResponseBody CommodityListBaseDto findCommodityList(String data) {
		QueryCondition queryCondition = JsonUtil.toBean(data, QueryCondition.class);
		CommodityListBaseDto<Commodity> commodityListBaseDto = new CommodityListBaseDto<Commodity>();
		if (queryCondition == null) {
			queryCondition = new QueryCondition();
		}
		try {
			List<Commodity> commodities = iCommodityServ.queryAll(queryCondition);
			commodityListBaseDto.setData(commodities);
			queryCondition.setPageSize(null);
			List<Commodity> commoditySize = iCommodityServ.queryAll(queryCondition);
			if(commoditySize != null) {
				commodityListBaseDto.setRecordsFiltered(commoditySize.size());
			}else {
				commodityListBaseDto.setRecordsFiltered(0);
			}
			commodityListBaseDto.setStatus(Constant.TRUE);
		} catch (Exception e) {
			// TODO: handle exception
			commodityListBaseDto.setStatus(Constant.FALSE);
			commodityListBaseDto.setMsg("查询失败");
		}
		return commodityListBaseDto;
	}

	/**
	 * 打包商品时, 查询已定价的单品列表 参数: queryCondition
	 * 
	 * @return
	 */
	@RequestMapping("findSingleCommodityList")
	public @ResponseBody CommodityListBaseDto findSingleCommodityList(String data) {
		QueryCondition queryCondition = JsonUtil.toBean(data, QueryCondition.class);
		CommodityListBaseDto<Commodity> commodityListBaseDto = new CommodityListBaseDto<Commodity>();
		if (queryCondition == null) {
			queryCondition = new QueryCondition();
			queryCondition.setProductDescId(1); // 查询单品信息
			queryCondition.setIsPrice(Constant.COMMODITY_IS_PRICE_B);
			queryCondition.setPageSize(null); // 不分页
		}
		try {
			commodityListBaseDto.setData(iCommodityServ.queryAll(queryCondition));
			commodityListBaseDto.setStatus(Constant.TRUE);
		}  catch (Exception e) {
			// TODO: handle exception
			commodityListBaseDto.setStatus(Constant.FALSE);
			commodityListBaseDto.setMsg("查询失败");
		}
		return commodityListBaseDto;
	}

	/**
	 * 查询商品详情
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping("findCommodityDetail")
	public @ResponseBody CommodityBaseDto<Commodity> findCommodityDetail(String data) {
		CommodityBaseDto<Commodity> baseDto = new CommodityBaseDto<Commodity>();
		Commodity commodity = null;
		Map<String, String> map = JsonUtil.toBean(data, Map.class);
		String id = (String) map.get("commodityId");
		try {
			// commodity = iCommodityServ.findById(id);
			Commodity findCommodityDetail = iCommodityServ.findCommodityDetail(id);
			baseDto.setData(findCommodityDetail);
			baseDto.setStatus(Constant.TRUE);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			baseDto.setStatus(Constant.FALSE);
			baseDto.setMsg("查询失败");
		}
		return baseDto;
	}

	/**
	 * 查询待审核商品信息
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping("findPendingAuditInfo")
	public @ResponseBody CommodityListBaseDto<Commodity> findPendingAuditInfo(String data) {
		QueryCondition queryCondition = JsonUtil.toBean(data, QueryCondition.class);
		CommodityListBaseDto<Commodity> commodityListBaseDto = new CommodityListBaseDto<Commodity>();
		try {
			commodityListBaseDto.setData(iCommodityServ.queryAll(queryCondition));
			commodityListBaseDto.setStatus(Constant.TRUE);
		} catch (Exception e) {
			// TODO: handle exception
			commodityListBaseDto.setStatus(Constant.FALSE);
			commodityListBaseDto.setMsg("查询失败");
		}
		return commodityListBaseDto;
	}

	/**
	 * 商品审核通过或者驳回 参数： commodityId:商品ID 状态： isPass: 1: 通过 -1: 驳回    rejectOpinion：  驳回理由
	 * 该审核为几审 :code 1:代表一审 2： 代表二审 （通过map取参）    auditor: 审核人
	 * 
	 * @return
	 */
	@RequestMapping("isPassed")
	public @ResponseBody PubOutputDto passedOrThrough(String data) {
		PubOutputDto pubOutputDto = new PubOutputDto();
		Map map = JsonUtil.toBean(data, Map.class);
		String commodityId = (String) map.get("commodityId");
		int isPass = (int) map.get("isPass");
		int code = (int) map.get("code");
		String  rejectOpinion = (String)map.get("rejectOpinion");
		String  auditor = (String)map.get("auditor");
		Commodity commodity = new Commodity();
		commodity.setId(commodityId);
		if (isPass == -1) {
			// 驳回
			if(code == 1) {
				commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_E); //一审驳回
			}else {
				commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_F); //二审驳回
			}
			commodity.setIsEdited(Constant.COMMODITY_IS_EDIT_A);
			commodity.setIsPrice(Constant.COMMODITY_IS_PRICE_A);
			commodity.setIsSubmitAudit(Constant.COMMODITY_IS_SUBMITAUDIT_A);
		} else {
			if (code == 1) {
				// 一审通过
				commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_C);
			} else {
				// 二审通过
				commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_D);
				commodity.setIsGrounding(Constant.COMMODITY_IS_GROUP_B);
				commodity.setIsPrice(Constant.COMMODITY_IS_PRICE_B);
			}
		}
		try {
			Auditrecord auditrecord = new Auditrecord();
			auditrecord.setAuditOpinion(rejectOpinion);
			auditrecord.setAuditor(auditor);
			commodity.setAuditrecord(auditrecord);
			iCommodityServ.updateById(commodity);
			pubOutputDto.setStatus(Constant.TRUE);
		} catch (Exception e) {
			// TODO: handle exception
			pubOutputDto.setStatus(Constant.FALSE);
			pubOutputDto.setMsg("服务器出错");
		}
		return pubOutputDto;
	}

	/**
	 * 导出数据
	 */
	@RequestMapping("exportCSV")
	public @ResponseBody CommodityListBaseDto<Commodity> exportCSV(String data) {
		CommodityListBaseDto<Commodity> commodityListBaseDto = new CommodityListBaseDto<Commodity>();
		QueryCondition queryCondition = JsonUtil.toBean(data, QueryCondition.class);
		if (queryCondition == null) {
			queryCondition = new QueryCondition();
		}
		try {
			List<Commodity> commoditys = iCommodityServ.queryAll(queryCondition);
			commodityListBaseDto.setData(commoditys);
			commodityListBaseDto.setStatus(Constant.TRUE);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			commodityListBaseDto.setStatus(Constant.TRUE);
		}
		return commodityListBaseDto;
	}

	/**
	 * 打包时提交单品
	 */
	@RequestMapping("commitSingleCommodityList")
	public @ResponseBody CommodityListBaseDto<CommodityProduct> commitSingleCommodity(String data) {
		CommodityListBaseDto<CommodityProduct> commodityListBaseDto = new CommodityListBaseDto<CommodityProduct>();
		List<String> ids = JsonUtil.toList(data, String.class);
		try {
			if (ids.size() > 0) {
				List<CommodityProduct> commitSingleCommodity = iCommodityServ.commitSingleCommodity(ids);
				commodityListBaseDto.setData(commitSingleCommodity);
			}
			commodityListBaseDto.setStatus(Constant.TRUE);
		} catch (Exception e) {
			// TODO: handle exception
			commodityListBaseDto.setStatus(Constant.FALSE);
			commodityListBaseDto.setMsg("显示失败");
		}
		return commodityListBaseDto;
	}
}
